package library.and.xps.com.step.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;

import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.AbstractRecyclerAdapter;
import library.and.xps.com.step.adapter.SearchAdapter;
import library.and.xps.com.step.m.model.Search;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.ListDividerItemDecoration;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseSwipeRefreshFragment;

public class SearchFragment extends BaseSwipeRefreshFragment implements AbstractRecyclerAdapter.OnRecyclerViewItemClickListener {
    private static final String Search = "Search";
    private String currentKeyWord = "";

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setBackgroundColor(Color.parseColor("#F0F0F0"));
        recyclerView.addItemDecoration(new ListDividerItemDecoration((int) (1 * getActivity().getResources().getDisplayMetrics().density)));
        abstractRecyclerAdapter = new SearchAdapter(activity, recyclerView);
        abstractRecyclerAdapter.setOnItemClickListener(this);
        setRefreshEnable(false);
        super.onActivityCreated(savedInstanceState);
    }

    public void doSearch(String content, int pageIndex) {
        if (currentKeyWord.equals(content)) return;

        currentKeyWord = content;
        onRefresh();
        String request = URLConstants.TEAM.SearchTeam + "?" + "tname=" + content + "&" + "page=" + pageIndex;
        OkHttpClientUtils.getDataAsync(getActivity(), request, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                onFail();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                String json = response.getBody();
                Logger.json(json);
                final Search search = new Gson().fromJson(json, Search.class);
                if (HttpResponsHelper.SuccessResponce(search)) {
                    adapterListData = search.getList();
                    onResponce();
                }
            }

            @Override
            public void onStart() {

            }
        }, Search);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpClientUtils.cancelCall(Search);
    }

    public void doLoadMore(int pageIndex) {
        onRefresh();
        String request = URLConstants.TEAM.SearchTeam + "?" + "tname=" + currentKeyWord + "&" + "page=" + pageIndex;
        OkHttpClientUtils.getDataAsync(getActivity(), request, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                onFail();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                String json = response.getBody();
                Logger.json(json);
                final Search search = new Gson().fromJson(json, Search.class);
                if (HttpResponsHelper.SuccessResponce(search)) {
                    adapterListData.addAll(search.getList());
                    onResponce();
                }
            }

            @Override
            public void onStart() {

            }
        }, Search);
    }

    @Override
    protected void getData(int pageIndex) {
        JUtils.Log("pageindex", pageIndex + "");
        int page = pageIndex + 1;
        doLoadMore(page);
    }

    @Override
    public void onItemClick(View view, int position) {
    }
}
